<?php
namespace app\admin\controller;
class Calculator extends Common
{
    /**
     * 展示-分期计算器
     */
	public function index1()
    {     
        $list1 = db('calculator_limit')     -> where(['type'=>1]) -> order('sort') -> select();
        $list2 = db('calculator_insurance') -> where(['type'=>1]) -> order('sort') -> select();
        $list3 = db('calculator_stage')     -> where(['type'=>1]) -> order('sort') -> select();
        $this -> assign('list1', $list1);
        $this -> assign('list2', $list2);
        $this -> assign('list3', $list3);
        return view();
    }

    /**
     * 展示-抵押计算器
    */
    public function index2()
	{
		$list1 = db('calculator_limit')     -> where(['type'=>2]) -> order('sort') -> select();
        $list2 = db('calculator_insurance') -> where(['type'=>2]) -> order('sort') -> select();
        $list3 = db('calculator_stage')     -> where(['type'=>2]) -> order('sort') -> select();
        $this -> assign('list1', $list1);
        $this -> assign('list2', $list2);
        $this -> assign('list3', $list3);
		return view();
	}
    
    /**
    * 添加-贷款额度管理
    */
	public function add1()
	{
        $type = input('param.type');
		if(request()->isPost())
		{
            $data = input('post.');
            $data['rate']=$data['rate']*0.01;
            $data['money']=$data['money']*0.01;
            $info = db('calculator_limit') -> insert($data);
            if ($info)
            {
            	$result['msg'] = '添加成功!';
	            $result['url'] = url('index'.$type);
	            $result['code'] = 1;
            } else 
            {
            	$result['msg'] = '添加失败!';
	            $result['url'] = url('add1',array('type'=>$type));
	            $result['code'] = 0;
            }
            return $result;
        }
        $this -> assign('type', $type);
		return view();
	}

    /**
    * 添加-保险三者
    */
    public function add2()
    {
        $type = input('param.type');
        if(request()->isPost())
        {
            $data = input('post.');
            $info = db('calculator_insurance') -> insert($data);
            if ($info)
            {
                $result['msg'] = '添加成功!';
                $result['url'] = url('index'.$type);
                $result['code'] = 1;
            } else 
            {
                $result['msg'] = '添加失败!';
                $result['url'] = url('add2',array('type'=>$type));
                $result['code'] = 0;
            }
            return $result;
        }
        $this -> assign('type', $type);
        return view();
    }

    /**
    * 添加-分期管理
    */
    public function add3()
    {
        $type = input('param.type');
        if(request()->isPost())
        {
            $data = input('post.');
            $info = db('calculator_stage') -> insert($data);
            if ($info)
            {
                $result['msg'] = '添加成功!';
                $result['url'] = url('index'.$type);
                $result['code'] = 1;
            } else 
            {
                $result['msg'] = '添加失败!';
                $result['url'] = url('add3',array('type'=>$type));
                $result['code'] = 0;
            }
            return $result;
        }
        $this -> assign('type', $type);
        return view();
    }









    

    /**
    * 编辑-贷款额度管理
    */
    public function edit1()
    {
        $type = input('param.type');


        $map['id'] = input('param.id');
        $info = db('calculator_limit')->where($map)->find(); 
        $info['rate']=$info['rate']*100;
        $info['money']=$info['money']*100;
        $this->assign('title',lang('edit'));
        $this->assign('info',$info);

        if(request()->isPost()) 
        {
            $data = input('post.');
            $data['rate']=$data['rate']*0.01;
            $data['money']=$data['money']*0.01;
            db('calculator_limit')->update($data);
            $result['msg'] = '修改成功!';
            $result['url'] = url('index'.$data['type']);
            $result['code'] = 1;
            return $result;
        } 
        $this -> assign('type', $type);
        return $this->fetch();
    }

    /**
    * 编辑-保险三者
    */
    public function edit2()
    {
        $type = input('param.type');
            $map['id'] = input('param.id');
            $info = db('calculator_insurance')->where($map)->find(); 
            $this->assign('title',lang('edit'));
            $this->assign('info',$info);

        if(request()->isPost()) 
        {
            $data = input('post.');
            db('calculator_insurance')->update($data);
            $result['msg'] = '修改成功!';
            $result['url'] = url('index'.$data['type']);
            $result['code'] = 1;
            return $result;
        }
        $this -> assign('type', $type);
        return $this->fetch();
    }

    /**
    * 编辑-分期管理
    */
    public function edit3()
    {
        $type = input('param.type');
        
            $map['id'] = input('param.id');
            $info = db('calculator_stage')->where($map)->find(); 
            $this->assign('title',lang('edit'));
            $this->assign('info',$info);

        if(request()->isPost()) 
        {
            $data = input('post.');
            db('calculator_stage')->update($data);
            $result['msg'] = '修改成功!';
            $result['url'] = url('index'.$data['type']);
            $result['code'] = 1;
            return $result;
        }
        $this -> assign('type', $type);
        return $this->fetch();
    }

    /**
    * 删除-分期计算器
    */
    public function del1()
    {
        $cate = input('post.cate');
        $id   = input('post.id');
        if ($cate == 1)
        {
            db('calculator_limit') -> delete($id);
        }
        if ($cate == 2)
        {
            db('calculator_insurance') -> delete($id);
        }
        if ($cate == 3)
        {
            db('calculator_stage') -> delete($id);
        }
        return ['code'=>1,'msg'=>'删除成功！'];
    }
   
}